package com.soft.javase.day09.map;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/4/29
 */
public class CharacterCount {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串：");
        String str = sc.nextLine();

        Map<Character, Integer> map = new HashMap<>();

        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            // 如果字符已存在于map中，则计数加1；否则初始化为1
            map.put(c, map.getOrDefault(c, 0) + 1);
        }

        System.out.println("字符出现次数统计：");
        for (Map.Entry<Character, Integer> entry : map.entrySet()) {
            System.out.println("'" + entry.getKey() + "': " + entry.getValue() + "次");
        }
    }
}
